package mn.slide.laborberlin.main;

import mn.slide.laborberlin.refresh.RefreshActivity;
import mn.slide.laborberlin.tabgroup.FavoriteGroupActivity;
import mn.slide.laborberlin.tabgroup.HistoryGroupActivity;
import mn.slide.laborberlin.tabgroup.HomeGroupActivity;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

public class TabBarActivity extends TabActivity {
	TabHost tabHost;
	Button back_btn;

	private String[] mStrings = {};
	public static int drawable = R.drawable.tab_refresh;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		SharedPreferences settings = getSharedPreferences("labor", 0);
		String formattedDate = settings.getString("first", "");
		if (formattedDate.equals("")) {
			new ShowDialog(this).execute();
		}
		SharedPreferences.Editor editor = settings.edit();
		editor.putString("first", "false");
		editor.commit();
		setTabs();
		//checkTime();
		back_btn = (Button) findViewById(R.id.back_btn);
		back_btn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				int groupIndex = getTabIndex();
				switch (groupIndex) {
				case 0:
					HomeGroupActivity.group.back();
					break;
				case 1:
					FavoriteGroupActivity.group.back();
					break;
				case 2:
					HistoryGroupActivity.group.back();
					break;

				default:
					break;
				}

			}
		});

	}

	private void setTabs() {
		addTab("Home", R.drawable.tab_home, HomeGroupActivity.class);
		addTab("Merken", R.drawable.tab_favorite, FavoriteGroupActivity.class);
		addTab("Verlauf", R.drawable.tab_history, HistoryGroupActivity.class);
		addTab("Refresh", drawable, RefreshActivity.class);
	}

	private void addTab(String labelId, int drawableId, Class<?> c) {
		tabHost = getTabHost();
		Intent intent = new Intent(this, c);
		TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);

		View tabIndicator = LayoutInflater.from(this).inflate(
				R.layout.tab_indicator, getTabWidget(), false);
		TextView title = (TextView) tabIndicator.findViewById(R.id.title);
		title.setText(labelId);
		ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
		icon.setImageResource(drawableId);

		spec.setIndicator(tabIndicator);
		spec.setContent(intent);
		tabHost.addTab(spec);
	}

	public int getTabIndex() {
		return tabHost.getCurrentTab();
	}

	@Override
	public void onBackPressed() {
		switch (tabHost.getCurrentTab()) {
		case 0:
			HomeGroupActivity.group.back();
			break;
		case 1:
			FavoriteGroupActivity.group.back();
			break;
		case 2:
			HistoryGroupActivity.group.back();
			break;

		default:
			break;
		}
	}

	public class ShowDialog extends AsyncTask<Void, Void, String[]> {

		Activity context;

		public ShowDialog(Activity context) {
			this.context = context;
		}

		@Override
		protected String[] doInBackground(Void... params) {

			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return mStrings;

		}

		@Override
		protected void onPostExecute(String[] result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			AlertDialog alertDialog = new AlertDialog.Builder(context).create();
			alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
			alertDialog.setMessage(getString(R.string.begin_dialog));
			alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					// here you can add functions
				}
			});
			alertDialog.show();
		}

	}

}
